package com.itqf.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * projectName: springframeworkgp8
 *
 * @author: 赵伟风
 * time: 2021/8/4 14:21
 * description:
 */

/**
 * 切面
 *   切点+本身就是增强 = 切面
 */
@Aspect
@Component("allAdvice")
public class AllAdvice {


    @Pointcut(value = "execution(* com..impl.*.*(..))")
    public void pc(){};


    /**
     * 前置增强
     * @param joinPoint 目标方法和目标对象的信息
     */
    @Before(value = "AllAdvice.pc()")
    public void before(JoinPoint joinPoint){
        String targetMethodName = joinPoint.getSignature().getName();
        String targetClassName = joinPoint.getTarget().getClass().getSimpleName();

        System.out.println("AllAdvice.before");
    }


    /**
     * 后置,不发生异常处理
     */
    @AfterReturning(value = "AllAdvice.pc()")
    public void afterReturning(JoinPoint joinPoint){
        System.out.println("AllAdvice.afterReturning");
    }

    /**
     * 后置,一定执行!
     */
    @After(value = "execution(* com..impl.*.*(..))")
    public void after(){
        System.out.println("AllAdvice.after");
    }

    /**
     * 异常增强方法
     * @param joinPoint 目标对象和方法信息
     * @param throwable 异常对象信息
     */
    @AfterThrowing(value = "execution(* com..impl.*.*(..))" ,throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint,Throwable throwable){
        String message = throwable.getMessage();
        System.out.println("message = " + message);
        System.out.println("AllAdvice.afterThrowing");
    }


    /**
     * 环绕通知
     * @param joinPoint 切点 确定,增强代码的执行位置,注意如果放在后面,异常不会触发!
     * @return Object 调用目标方法的返回值!
     */
    @Around(value = "execution(* com..impl.*.*(..))")
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("AllAdvice.round11");

        Object result  = joinPoint.proceed();
        System.out.println("AllAdvice.round22");
        return result;
    }

}
